package com.mapper.living;


import living.com.domain.FansUser;
import living.com.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * @author dhy
 */
@Mapper
public interface UserDaoBak {
  /**
   * 通过用户id查找用户
   * @param id
   * @return
   */
  @Select("SELECT u.*,(SELECT COUNT(*) FROM user_verify WHERE is_passed=1 AND " +
          "user_id=#{id}) is_verify,(SELECT COUNT(*) FROM user_vip WHERE user_id=14 AND deadline > NOW()) is_vip \n" +
          "FROM `user` u \n" +
          "WHERE u.id=#{id};")
  User doGetUserById(int id);

  /**
   * 返回当前文章或评论发布者的信息,并判断当前用户是否关注该发布者
   * @param user_id  当前文章,或者评论发布者的id
   * @param attention_id 对应当前用户的id
   */
  @Select("SELECT u.*,uv.is_passed is_verify,(SELECT COUNT(*) FROM user_vip WHERE user_id=14 AND deadline > NOW()) is_vip ,\n" +
          "(SELECT COUNT(*) FROM user_attention WHERE user_id =#{user_id} AND attention_user_id=#{attention_id}) is_attention\n" +
          "FROM `user` u \n" +
          "INNER JOIN user_verify uv \n" +
          "ON u.id=uv.user_id\n" +
          "WHERE u.id=#{user_id}")
   User publishWithAttention(@Param("user_id") int user_id, @Param("attention_id") int attention_id);

  /**
   *  当前用户关注该文章或评论的发布者
   * @param user_id  当前文章,或者评论发布者的id
   * @param attention_user_id  对应当前用户的id
   * @return
   */
  @Insert("INSERT INTO user_attention(user_id,attention_user_id) VALUES(#{user_id},#{attention_user_id});")
  Boolean attentionTargetUser(@Param("user_id") int user_id, @Param("attention_user_id") int attention_user_id);


  /**
   * 获取粉丝列表
   * @param userId
   * @return
   */
  @Select("SELECT id,username,avatar,(SELECT COUNT(*) FROM user_attention WHERE user_id = #{userId} AND attention_user_id = user.id LIMIT 1) "
          + "AS mutual FROM user WHERE id IN (SELECT user_id FROM user_attention WHERE attention_user_id =#{userId})")
  List<FansUser> getFansList(int userId);

  /**
   *  插入举报记录
   */
  @Insert("INSERT INTO user_complaint (informer_id,object_id,reason,object_type) VALUES (#{informerId},#{objectId},#{reason},#{objectType})")
  int inform(@Param("informerId") int informerId,@Param("objectId") int objectId,@Param("reason") String reason,@Param("objectType") String objectType);
}